/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月29日         luwei
 */

package com.coocaa.launcher.sidebar.left.item.appcircle;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;

import com.coocaa.launcher.framework.manager.configdata.data.AppStoreNum;
import com.coocaa.launcher.framework.sidebar.item.Item;
import com.coocaa.launcher.framework.sidebar.item.ItemView;
import com.coocaa.launcher.settings.appstoreupdate.AppStoreNews;
import com.coocaa.launcher.settings.appstoreupdate.AppStoreNews.AppStoreBack;
import com.coocaa.launcher.sidebar.SettingData;
import com.coocaa.launcher.util.PathConstants;
import com.coocaa.x.framework.utils.LogUtils;
import com.skyworth.logsdk.appstore.LogAPIForAppStore;
import com.tianci.appstore.AppStoreActivity;
import com.tianci.appstore.utils.SkyConstants;

public class AppCircle extends Item implements AppStoreBack {
    private AppStoreNews mAppStoreNews = null;
    private AppStoreNum mNum = null;
    private SettingData mSettingData = null;

    public AppCircle(Context context) {
        super(context);
        mAppStoreNews = new AppStoreNews();
        mAppStoreNews.setAppStoreBack(this);
        mAppStoreNews.getAppStoreUpdateNum();
    }

    @Override
    public void onItemFocusChange(View v, boolean focus) {
        super.onItemFocusChange(v, focus);
    }

    @Override
    public boolean onItemKeyEvent(View v, int keycode, KeyEvent keyEvent) {
        return false;
    }

    @Override
    public void onItemClick(View v) {
        Intent intent = new Intent(mContext, AppStoreActivity.class);
        try {
            mContext.startActivity(intent);
        } catch (Exception e) {
            LogUtils.i("" + e);
        }

        ((AppCircleItem) mItemView).hideRightView();
        if (mNum != null) {
            mAppStoreNews.saveNewAppsNum(mNum.version);
        }
        LogAPIForAppStore.appStoreStartPath(PathConstants.LaunchAppStorePath.LEFT_MENU.toString());
        super.onItemClick(v);
    }

    @Override
    public ItemView createItemView() {
        // TODO Auto-generated method stub
        return new AppCircleItem(mContext);
    }

    @Override
    public void onUpdateNum(AppStoreNum num) {
        mNum = num;
        if (mNum != null && mNum.num > 0) {
            ((AppCircleItem) mItemView).showRightView();
            ((AppCircleItem) mItemView).setAppNewsNum(String.valueOf(mNum.num));
        }
    }

    @Override
    public void setData(Object object) {
        this.mSettingData = (SettingData) object;
        ((AppCircleItem) mItemView).updateIcon(SkyConstants.getIconId(mSettingData.iconId));
        ((AppCircleItem) mItemView).updateName(SkyConstants.getStringId(mSettingData.titleId));
    }
}
